package com.cn.dream_order_server.async;

import com.cn.dream_order_server.pojo.OrderShopping;
import com.cn.dream_order_server.service.OrderCartService;
import com.cn.dream_order_server.util.JsonUtils;
import com.cn.dream_order_server.vo.ToCheckoutlVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.Future;

@Async
@Component
public class OrderCartAsync {


    @Autowired
    private OrderCartService orderCartService;


    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    public void  wlcBeforeTheSettlementUpdateShoppingCart(List<ToCheckoutlVO> list){
        //遍历传进来的list
        for (ToCheckoutlVO  toCheckoutlVO:list) {
            //根据key去list取购物车对象
            String s = stringRedisTemplate.opsForValue().get("orderCart-" + toCheckoutlVO.getShoppingId());
            //判断对象是否存在
            if (s!=null&&!"".equals(s)){
                //存在则跟新数据库
                orderCartService.updateOrderCartByShoppingId(JsonUtils.stringToObj(s,OrderShopping.class));
                //修改完毕后删除rediskey
                stringRedisTemplate.delete("orderCart-" + toCheckoutlVO.getShoppingId());
            }
        }
    }


    public Future<List<OrderShopping>> wlcGetOrderShoppingListInshoppingId(String[] shoppingIds){
        List<OrderShopping> list = orderCartService.selectOrderShoppingListInOrderShoppingId(shoppingIds);
        return new AsyncResult<List<OrderShopping>>(list);
    }
}
